package com.sunyard.pcc.web.controller;

import com.sunyard.michiru.common.entity.BaseResult;
import com.sunyard.michiru.common.entity.ErrorCodeEnum;
import com.sunyard.pcc.common.bo.SysMenuBO;
import com.sunyard.pcc.common.validator.ValidationUtils;
import com.sunyard.pcc.core.annotation.SysLog;
import com.sunyard.pcc.core.service.MenuService;
import com.sunyard.pcc.web.form.MenuForm;
import com.sunyard.pcc.web.vo.MenuVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Hewei
 * @Description: 菜单模块对应控制器
 * @Date: Created in 14:23 2018/4/10
 */
@Slf4j
@RestController
@RequestMapping(value = "menu")
public class MenuController {

    @Autowired
    private MenuService menuService;



    @SysLog(operation = "添加新菜单")
    @ApiOperation(value = "添加新菜单")
    @ApiImplicitParam(name = "menuForm", value = "新菜单数据", paramType = "body", dataType = "MenuForm")
    @PostMapping(value = "/newMenu")
    public BaseResult<Boolean> newMenu(@RequestBody MenuForm menuForm) {

        //参数校验
        ValidationUtils.validateEntity(menuForm);

        SysMenuBO sysMenuBO = new SysMenuBO();
        BeanUtils.copyProperties(menuForm, sysMenuBO);

        Boolean result = menuService.addNewMenu(sysMenuBO);

        return BaseResult.create(result, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
    }


    @SysLog(operation = "删除菜单")
    @ApiOperation(value = "删除菜单")
    @ApiImplicitParam(name = "menuId", value = "菜单Id", paramType = "path", dataType = "Long")
    @DeleteMapping(value = "/{menuId}")
    public BaseResult<Boolean> deleteMenu(@PathVariable Long menuId) {
        //非空判断
        ValidationUtils.isBlank(menuId);

        Boolean result = menuService.deleteMenuById(menuId);

        return BaseResult.create(result, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
    }


    @SysLog(operation = "更新菜单")
    @ApiOperation(value = "更新菜单数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单Id", paramType = "path", dataType = "Long"),
            @ApiImplicitParam(name = "menuForm", value = "更新菜单数据", paramType = "body", dataType = "MenuForm")
    })
    @PatchMapping(value = "/{menuId}")
    public BaseResult<Boolean> updateMenu(@PathVariable Long menuId, @RequestBody MenuForm menuForm) {
        //参数校验
        ValidationUtils.validateEntity(menuForm);

        SysMenuBO sysMenuBO = new SysMenuBO();
        BeanUtils.copyProperties(menuForm, sysMenuBO);
        Boolean result = menuService.updateMenu(menuId, sysMenuBO);

        return BaseResult.create(result, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
    }


    /**
     * 菜单树形列表查询，菜单列表不设置分页，无条件查询
     * @return
     */
    @SysLog(operation = "查询树状菜单列表")
    @ApiOperation(value = "查询菜单树状列表")
    @GetMapping(value = "/menuTree")
    public BaseResult<List<MenuVO>> getMenuTree(){


        //返回树形结构数据
        List<SysMenuBO> menuBOS = menuService.getMenuListInTreeMode();
        //菜单数量
        Integer totalCount = menuService.menuListCounts(menuBOS);
        //BO TO VO
        List<MenuVO> menuVOS = menuBOListToVOList(menuBOS);
        BaseResult<List<MenuVO>> result  = BaseResult.create(menuVOS, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        result.setTotalCount(totalCount);

        return result;
    }


    /**
     * 菜单列表查询
     * @return
     */
    @SysLog(operation = "查询菜单列表")
    @ApiOperation(value = "查询菜单列表")
    @GetMapping(value = "/menuList")
    public BaseResult<List<MenuVO>> getMenuList() {
        List<SysMenuBO> menuBOs = menuService.getMenuList();
        List<MenuVO> menus = menuBOListToVOList(menuBOs);
        BaseResult<List<MenuVO>> result = BaseResult.create(menus, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        result.setTotalCount(menus.size());
        return result;
    }


    /**
     * 递归复制menuBOs to MenuVOs
     * @param menuBOS
     * @return
     */
    private List<MenuVO> menuBOListToVOList (List<SysMenuBO> menuBOS){
        List<MenuVO> menuVOs = new ArrayList<>();
        for (SysMenuBO menuBO: menuBOS){
            MenuVO menuVO = new MenuVO();
            BeanUtils.copyProperties(menuBO, menuVO);
            List<SysMenuBO> childMenuBOS = menuBO.getChildMenus();
            menuVOs.add(menuVO);
            if (CollectionUtils.isEmpty(childMenuBOS)) {
                continue;
            } else {
                List<MenuVO> childMenuVOS = menuBOListToVOList(childMenuBOS);
                menuVO.setChildMenus(childMenuVOS);
            }
        }

        return menuVOs;
    }




}
